package com.dataimport.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.ScientificPapersInfo;
import com.dataimport.entity.targetentity.ResearchPaper;
import com.dataimport.mapper.ResearchPaperMapper;
import com.dataimport.mapper.ScientificPapersInfoMapper;
import com.dataimport.service.ResearchPaperService;
import com.dataimport.utils.HasNullFieldUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 卑微小峰
 * @date 2022/9/27
 */
@Service
public class ResearchPaperServiceImpl extends ServiceImpl<ResearchPaperMapper, ResearchPaper> implements ResearchPaperService {

    private static long PAGE_SIZE = 1000;

    @Autowired
    private ScientificPapersInfoMapper scientificPapersInfoMapper;

    @Override
    public void dealWithResearchPaper() {
        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<ResearchPaper> researchPaperList = selectData(currentPage);
            if (ObjectUtils.isEmpty(researchPaperList)) {
                break;
            }
            insertData(researchPaperList);
        }
    }

    public List<ResearchPaper> selectData(long currentPage) {
        DynamicDataSourceContextHolder.push("mysql2");
        IPage<ScientificPapersInfo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(PAGE_SIZE);
        IPage<ScientificPapersInfo> data = scientificPapersInfoMapper.selectPage(page, null);
        DynamicDataSourceContextHolder.poll();

        List<ScientificPapersInfo> scientificPapersInfoList = data.getRecords();
        List<ResearchPaper> researchPaperList = scientificPapersInfoList.stream()
                .filter(scientificPapersInfo -> HasNullFieldUtil.inExistenceNull(scientificPapersInfo) && scientificPapersInfo.getLwfbrq().length() == 8)
                .map(scientificPapersInfo -> {
                    ResearchPaper researchPaper = new ResearchPaper();
                    researchPaper.setPublishTime(DateUtil.parse(scientificPapersInfo.getLwfbrq()));
                    BeanUtils.copyProperties(scientificPapersInfo, researchPaper);
                    return researchPaper;
                })
                .collect(Collectors.toList());

        return researchPaperList;
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertData(List<ResearchPaper> researchPaperList) {
        try {
            DynamicDataSourceContextHolder.push("mysql1");
            this.saveBatch(researchPaperList, researchPaperList.size());
            DynamicDataSourceContextHolder.poll();
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
